package yja.gzw.app.activity

import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import yja.gzw.app.R
import yja.gzw.app.adapter.JingpingAdaptzer
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityJinpingshipingBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import com.google.android.material.tabs.TabLayout
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import yja.gzw.app.adapter.Fabudingdan2Adapter
import yja.gzw.app.model.bean.OrderDatajingpingshiping

class JinpingshipingActivity :BaseActivity<Mainviewmodel, ActivityJinpingshipingBinding>(R.layout.activity_jinpingshiping){

    var nenene=""
    var yeshu=1
    var zhuangtai="all"
    var shuju= mutableListOf<OrderDatajingpingshiping>()

    var listtlit= mutableListOf<String>("全部","已出价","待第二方出价","第二方竞价")
    private lateinit var fabiada: JingpingAdaptzer



    override fun initView() {

        binding.sousuo.setOnClickListener {

            val intent=Intent(this,SousoujingpingActivity::class.java)
            startActivity(intent)
        }


        binding.fabuanniu.setOnClickListener {
            val intent= Intent(this,JingpingshipingfabuActivity::class.java)
            startActivity(intent)
        }

        binding.fanhui.setOnClickListener {
            finish()
        }

        for (i in 0  until listtlit.size ){
            val tab = binding.tabs.newTab()
            binding.tabs.addTab(tab.setText(listtlit[i]))
        }

        viewmodel.onjingpingshipingwang(UserData.token, mapOf("keyword" to "","status" to "all","page" to "1","pagesize" to "10"))

        binding.tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {


                if(tab.position==0){
                    zhuangtai="all"
                    viewmodel.onjingpingshipingwang(UserData.token, mapOf("keyword" to "","status" to "all","page" to "1","pagesize" to "10"))


                }else if(tab.position==1){
                    zhuangtai="1"
                    viewmodel.onjingpingshipingwang(UserData.token, mapOf("keyword" to "","status" to "1","page" to "1","pagesize" to "10"))


                }else if(tab.position==2){
                    zhuangtai="2"
                    viewmodel.onjingpingshipingwang(UserData.token, mapOf("keyword" to "","status" to "2","page" to "1","pagesize" to "10"))


                }else{
                    zhuangtai="3"
                    viewmodel.onjingpingshipingwang(UserData.token, mapOf("keyword" to "","status" to "3","page" to "1","pagesize" to "10"))
                }


            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })

        binding.refreshLayout.setRefreshHeader(ClassicsHeader(this));
        binding.refreshLayout.setRefreshFooter(ClassicsFooter(this));
        binding.refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                shuju.clear()
                yeshu=1
                viewmodel.onjingpingshipingwang(UserData.token, mapOf("keyword" to "","status" to zhuangtai,"page" to "1","pagesize" to "10"))

            }


        })

        binding.refreshLayout.setOnLoadMoreListener(object : OnLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {

                if(yeshu==((shuju.size/10)+1)){

                }else{
                    yeshu++
                }
                viewmodel.onjingpingshipingwang(UserData.token, mapOf("keyword" to "","status" to zhuangtai,"page" to yeshu.toString(),"pagesize" to "10"))


            }


        })




    }

    override fun dataObservese() {


        viewmodel.onjingpingshiping.observe(this, Observer {
            if(it.code==1){

                if(it.data.data.size<10&&yeshu==1){

                    binding.review.layoutManager=
                        LinearLayoutManager(this)
                    fabiada= JingpingAdaptzer(this,it.data.data)
                    binding.review.adapter= fabiada
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()

                }else{
                    shuju.addAll(it.data.data)
                    binding.review.layoutManager=
                        LinearLayoutManager(this)
                    fabiada= JingpingAdaptzer(this,shuju)
                    binding.review.adapter= fabiada
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()
                    if(yeshu==1){
                        binding.review.scrollToPosition(0)

                    }else{
                        val shshbu=10*(yeshu-1)
                        binding.review.scrollToPosition(shshbu-2)

                    }
                }




            }else{

                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }


        })


//        viewmodel.onjingpingshiping.observe(this, Observer {
//            if(it.code==1){
//                    Log.e("精品视频数据",it.data.data.toString())
//
//                binding.review.layoutManager=
//                    LinearLayoutManager(this)
//                fabiada= JingpingAdaptzer(this,it.data.data)
//                binding.review.adapter= fabiada
//
//            }else{
//                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
//
//            }
//
//
//        })


    }
}